package dk.au.transxor;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;

import dk.au.transxor.message.DirectMessage;
import dk.au.transxor.message.MessageFactory;
import dk.au.transxor.mote.ContinuousFileSender;
import dk.au.transxor.mote.FileReceiver;
import dk.au.transxor.mote.FileSender;
import dk.au.transxor.mote.MoteUart;
import dk.au.transxor.mote.ThrottleFileSender;

public class Main {

	// Lasse 	0xa (mote: )
	// Lars 	0xb (mote: 012)
	
	// Input files
	public static final String TextInput = "textfile.txt";
	public static final String PictureInput = "InputA.bmp";
	
	// Output files
	public static final String TextOutput = "outputtext.txt";
	public static final String PictureOutput = "outputPic.bmp";
	
	// Formatted in-/out-put files
	public static final String FilePath = "files/";
	
//	public static final String InputFile = FilePath + PictureInput;
//	public static final String OutputFile = PictureOutput;
	public static final String InputFile = FilePath + PictureInput;
	public static final String OutputFile = PictureOutput;
	
	public static final int DefaultMoteDestination = 0xc; // Or 13
	public static final int SendDelay = 0;
	public static final int AmType = MessageFactory.DirectMessasge_AmType;
	public static final int MaxDataLength = DirectMessage.numElements_data_field();
	public static final boolean ThrottleMode = true;

	public static void main(String[] args) throws Exception {
		
		//fileSendTerminal();
		//fileReceiveTerminal();
		
		fullDuplexMode();
	}
	
	public static void fullDuplexMode() {

		try {

			MoteUart moteUart = new MoteUart();
	
			File file = new File(FilePath + OutputFile);
	
			FileReceiver receiver = new FileReceiver(MessageFactory.createMessage(AmType), MaxDataLength, moteUart);
			
			receiver.receiveFile(file);
			
			System.out.println("Waiting for file!");
			
			file = new File(InputFile);

			FileSender sender = null;
			
			if(ThrottleMode)
				sender = new ThrottleFileSender(AmType, MaxDataLength,moteUart, DefaultMoteDestination, SendDelay);
			else
				sender = new ContinuousFileSender(AmType, MaxDataLength,moteUart, DefaultMoteDestination, SendDelay);

			System.out.println("Press enter to start sending:");
			BufferedReader consoleInput = new BufferedReader(new InputStreamReader(System.in));
			consoleInput.readLine();
						
			System.out.println("Sending file!");
			
			sender.sendFile(file, false);
			
			System.out.println("File sent!");
			
			while(true) {
				Thread.sleep(1000);
			}
		
		} catch (FileNotFoundException e1) {
			e1.printStackTrace();
		} catch (InterruptedException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public static void fileReceiveTerminal() {

		try {

			// Setup receiver
			MoteUart moteUart = new MoteUart();

			File file = new File(FilePath + OutputFile);

			FileReceiver receiver = new FileReceiver(MessageFactory.createMessage(AmType), MaxDataLength, moteUart);

			System.out.println("Waiting for messages!");

			receiver.receiveFile(file);
			

		} catch (FileNotFoundException e1) {
			e1.printStackTrace();
		}

	}

	public static void fileSendTerminal() {

		try {
			
			MoteUart moteUart = new MoteUart();

			File file = new File(InputFile);

			FileSender sender = new ContinuousFileSender(AmType, MaxDataLength,moteUart, DefaultMoteDestination, SendDelay);

			sender.sendFile(file, false);

			moteUart.stop();

			System.out.println("Stopped!");
			
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}

	}
}
